package com.plant.action;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;
import com.plant.entity.PgcArticle;
import com.plant.entity.UgcCollect;
import com.plant.entity.UgcUser;
import com.plant.service.FindService;
import com.plant.tool.DateUtils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller                                                                     
@RequestMapping(value = "/discover1_0_1")    
public class DiscoverContraller {
	@Resource(name = "findServiceImpl")
	private FindService findService;

	@RequestMapping(value = "list_allcontents")//查看所有的文章和
	@ResponseBody                                                               
	public Map<String,Object> AllContents(Integer page,HttpServletResponse response,HttpServletRequest request,String secret,String pust_val) throws BadElementException, MalformedURLException, IOException {
		response.setHeader("access-control-allow-origin","*");	
		Map<String, Object> map1=new HashMap<String, Object>();
		if(secret==null||pust_val==null||page==null){
			map1.put("state", "0");
			map1.put("message", "参数不能为空");
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Map<String, Object>> allcontents=new ArrayList<Map<String,Object>>();
				allcontents=findService.ListAllContents(page);
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				for (Map<String, Object> map : allcontents) {
					String url=map.get("image").toString();
					Image img = Image.getInstance(new URL(url)); 
					map.put("width", img.getWidth());
					map.put("height", img.getHeight());
					Object obj=map.get("create_time");
					String time=sim.format(obj);
					map.put("create_time", time);
				}
				if(allcontents.size()>0){
					map1.put("allcontents", allcontents);
					map1.put("state", 1);
					map1.put("message", "查询成功");
				}else{
					map1.put("allcontents", allcontents);
					map1.put("state", 0);
					map1.put("message", "查询失败");	
				}
				return map1;
			}else{
				map1.put("state", 0);
				map1.put("message", "秘钥错误");
				return map1;
			}
		}
	}

	@RequestMapping(value = "list_mycollect") //2查看我的收藏
	@ResponseBody                                                               
	public Map<String,Object> ListMyCollect(String time,Integer page,Integer user_id,UgcCollect uc,HttpServletResponse response,String secret,String pust_val) throws BadElementException, MalformedURLException, IOException{
		response.setHeader("access-control-allow-origin","*");	
		Map<String, Object> map1=new HashMap<String, Object>();
		if(secret==null||pust_val==null||user_id==null||time==null||page==null){
			System.out.println(pust_val+secret+user_id+time);
			map1.put("state", "0");
			map1.put("message", "参数不能为空");
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				uc.setUserId(user_id);
				List<Object[]> list=new ArrayList<Object[]>();
				List<Object[]> list1=new ArrayList<Object[]>();
				List<Map<String, Object>> content=new ArrayList<Map<String,Object>>();
				list=findService.MyCollect(uc,time,page);
				list1=findService.MyCollect(uc,time,page+1);
				if(list1.size()>0){
					map1.put("hasmore", 1);
				}else{
					map1.put("hasmore", 0);
				}
				int len=list.size();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				if(len>0){
					for (int i = 0; i < len; i++) {
						List<Map<String, Object>> temp=new ArrayList<Map<String,Object>>();
						Object[] obj=list.get(i);
						if(obj[1].equals(1)){
							temp=findService.ArticleInf(obj[0]);
							for (Map<String, Object> map : temp) {
								map.put("content_type", "1");
								String image=map.get("image").toString();
								Image img = Image.getInstance(new URL(image)); 
								map.put("height", img.getHeight()); //--获取图片的高度
								map.put("width", img.getWidth()); 
								map.put("is_like", "1");
								map.put("comment_count", "123");
								map.put("collect_time", DateUtils.StaticconvertTimeToFormat(DateUtils.StaticgetUnixStamps(sim.format(obj[2]))));
							}
							content.addAll(temp);
						}else{
							temp=findService.TopicInf(obj[0]);
							for (Map<String, Object> map : temp) {
								map.put("content_type", "2");
								String image=map.get("image").toString();
								Image img = Image.getInstance(new URL(image)); 
								map.put("height", img.getHeight()); //--获取图片的高度
								map.put("width", img.getWidth()); 
								map.put("is_like", "1");
								map.put("comment_count", "123");
								map.put("collect_time", DateUtils.StaticconvertTimeToFormat(DateUtils.StaticgetUnixStamps(sim.format(obj[2]))));
							}
							content.addAll(temp);
						}
					}
				}   	
				map1.put("content", content);
				return map1;
			}else{
				map1.put("state", 0);
				map1.put("message", "秘钥错误");
				return map1;
			}
		}
	}

	@RequestMapping(value = "list_tags")//查看显示搜索标签
	@ResponseBody                                                               
	public Map<String,Object> ListSearchTags(HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");	
		Map<String, Object> map1=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map1.put("state", "0");
			map1.put("message", "参数不能为空");
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Map<String, Object>> tags=new ArrayList<Map<String,Object>>();
				tags=findService.ListSearchTags();
				if(tags.size()>0){
					map1.put("tags", tags);
					map1.put("message", "查询成功");
				}else{
					map1.put("tags", tags);
					map1.put("message", "查询列表为空 请联系后端工程师");
				}
				return map1;
			}else{
				map1.put("state", 0);
				map1.put("message", "秘钥错误");
				return map1;
			}
		}
	}

	
	@RequestMapping(value="article")//---2.查看显示官方文章
	@ResponseBody
	public Map<Object,Object> pgcArticle(HttpServletResponse response,PgcArticle pa,UgcUser user,Integer page,String time,HttpServletRequest request,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||page==null||time==null){
			map1.put("message", "参数不能为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> list2=new ArrayList<Map<String,Object>>();
				list=findService.listArticle(page, time);	//1.查看显示官方文章
				list2=findService.listArticle(page+1, time);//1.查看显示官方文章
				if(list2.size()>0){
					map1.put("hasmore", 1);
				}else{
					map1.put("hasmore", 0);
				}
				SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd");
				for (Map<String, Object> map : list) {
					String image=map.get("image").toString();
					try {
						Image img = Image.getInstance(new URL(image));
						map.put("height", img.getHeight()); //--获取图片的高度
						map.put("width", img.getWidth());   //--获取图片的宽度
					} catch (Exception e) {
					}
					String stime=map.get("add_time").toString();
					map.put("add_time", sim.format(DateUtils.StaticgetUnixStamps(stime)));
				}
				map1.put("article", list);
				map1.put("message", "查询推荐文章列表成功");
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	} 

	@RequestMapping(value="activity")//查看显示官方活动
	@ResponseBody
	public Map<Object,Object> PCActivity(HttpServletResponse response,PgcArticle pa,UgcUser user,Integer page,
			String time,HttpServletRequest request,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||page==null||time==null){
			map1.put("message", "参数不能为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> list2=new ArrayList<Map<String,Object>>();
				list=findService.listActivity(page, time);
				list2=findService.listActivity(page+1, time);
				if(list2.size()>0){
					map1.put("hasmore", 1);
				}else{
					map1.put("hasmore", 0);
				}
				for (Map<String, Object> map : list) {
					String image=map.get("image").toString();
					try {
						Image img = Image.getInstance(new URL(image));
						map.put("height", img.getHeight()); //--获取图片的高度
						map.put("width", img.getWidth());   //--获取图片的宽度
					} catch (Exception e) {
					}
				}
				map1.put("article", list);
				map1.put("message", "查询活动成功");
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	} 


}
